#include "../include/easylog.hpp"
#include <iostream>
#include <cassert>

using namespace easylog;

void test_registry()
{
    logger_builder::ptr llb = std::make_shared<local_logger_builder>();
    llb->build_logger_type(logger_builder::sync);
    llb->build_logger_name("test");
    llb->build_limit_level(level::debug);
    llb->build_sinker<stdout_sinker>();

    add(llb->build());
    //assert(has("test"));
    logger::ptr lgr = get("test");

    lgr->info("{}:{}", "test", 1);
    lgr->info("%s:%d", "test", 1);

    lgr = default_logger();
    lgr->info("{}:{}", "test", 1);
    lgr->info("%s:%d", "test", 1);

    EASYLOG_LOGGER_DEBUG(lgr, "{}", "test easylog_LOGGER_DEBUG");
}

void test_api()
{
    debug("{}:{}", "test", 1);
    debug("%d:%d", "test", 1);
    info("{}:{}", "test", 1);
    info("%d:%d", "test", 1);
    warn("{}:{}", "test", 1);
    warn("%d:%d", "test", 1);
    error("{}:{}", "test", 1);
    error("%d:%d", "test", 1);
    fatal("{}:{}", "test", 1);
    fatal("%d:%d", "test", 1);
}

void test_marco()
{
    EASYLOG_DEBUG("{}:{}", "test", 1);
    EASYLOG_DEBUG("%d:%d", "test", 1);
    EASYLOG_INFO("{}:{}", "test", 1);
    EASYLOG_INFO("%d:%d", "test", 1);
    EASYLOG_WARN("{}:{}", "test", 1);
    EASYLOG_WARN("%d:%d", "test", 1);
    EASYLOG_ERROR("{}:{}", "test", 1);
    EASYLOG_ERROR("%d:%d", "test", 1);
    EASYLOG_FATAL("{}:{}", "test", 1);
    EASYLOG_FATAL("%d:%d", "test", 1);
}

void test_config_01()
{
    set_level(level_enum::error);
    set_pattern("test set pattern : %m%n");
    set_colorful(true);
    set_color({
        { level_enum::error, color_enum::light_green },
    });
    info("test info");
    error("test error");
}

void test_config_02()
{
    set_fn_pattern("logs/testsetestttestest.log");
    logger_builder::ptr llb = std::make_shared<local_logger_builder>();
    llb->build_logger_type(logger_builder::sync);
    llb->build_logger_name("test");
    llb->build_limit_level(level::debug);
    llb->build_sinker<file_sinker>();
    logger::ptr lgr = llb->build();
    lgr->info("test set_fn_pattern");
}

void test_fatal()
{
    //easylog::fatal("sync get a fatal");

    logger_builder::ptr llb = std::make_shared<local_logger_builder>();
    llb->build_logger_type(logger_builder::async);
    llb->build_logger_name("async_test");
    llb->build_limit_level(level::debug);
    llb->build_sinker<stdout_sinker>();
    llb->build_work_type(logger_builder::work_type::safe);
    llb->build_thread_num();
    llb->build_buffer_size();
    llb->build_buffer_increment();
    llb->build_buffer_threshold();
    logger::ptr lgr = llb->build();
    lgr->fatal("async get a fatal");
    sleep(1);
}

#define TEST \
                6

int main()
{
    switch (TEST)
    {
        case 1: test_registry(); break;
        case 2: test_api(); break;
        case 3: test_marco(); break;
        case 4: test_config_01(); break;
        case 5: test_config_02(); break;
        case 6: test_fatal(); break;
        default: assert(false);
    }

    return 0;
}
